<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/home.png" />
        <span>系统</span>
      </div>
      <el-button type="warning" @click="logout">退出登录</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="foldStatus ? '60px': '200px'">
        <!-- 菜单区域 -->
        <div class="fold-button" @click="foldOrNo">折叠/展开</div>
        <el-menu
          background-color="#166370"
          text-color="#fff"
          active-text-color="yellow"
          :unique-opened="true"
          :collapse="foldStatus"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 属性 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]">{{space}}</i>
              <!-- 菜单文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 菜单文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <!-- 子组件占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      space: ' ',
      // 是否折叠 状态码
      foldStatus: false,
      // 被激活的路由地址 (当前选择的导航栏)
      activePath: ''
    }
  },
  created() {
    // 获取所有菜单
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单方法
    async getMenuList() {
      // 解构获取
      const { data: res } = await this.$http.get('menus')
      // 失败
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
    },
    // 展开/折叠  切换按钮
    foldOrNo() {
      this.foldStatus = !this.foldStatus
    },
    // 保存当前选择的路径 刷新时方便回复
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scope>
.el-header {
  background-color: rgb(18, 60, 71);
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  img {
    width: 80px;
    height: 50px;
    border-radius: 2%;
    background-color: #eeeeee;
    box-shadow: 0 0 10px #dddddd;
  }
}
.el-aside {
  background-color: #166370;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}

.fold-button {
  background-color: #4a5064;
  font-size: 10px;
  color: #fff;
  line-height: 20px;
  text-align: center;
  letter-spacing: 1em;
  cursor: pointer;
}
</style>
